package com.bailiban.controller;

import com.bailiban.entity.HistoryData;
import com.bailiban.service.HistoryService;
import com.bailiban.utils.Result;
import com.bailiban.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zhulang
 * @Classname HistoryDataController
 * @Description TODO
 * @Date 2020/3/25 15:24
 */
@RestController
@RequestMapping("/historyData")
public class HistoryDataController {
    @Autowired
    private HistoryService historyService;

    /**
     * 查询指定省份的所有数据
     * @param provinceId
     * @return
     */
    @GetMapping("/all/provinceId/{provinceId}")
    public Result findAllByProvinceId(@PathVariable("provinceId") Integer provinceId){
        List<HistoryData> all = historyService.findAllByProvinceId(provinceId);
        return Result.success(all);
    }
    @GetMapping("/all/date/{date}")
    public Result findAllByDate(@PathVariable("date") String date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        Date parse=null;
        try {
             parse = dateFormat.parse(date);
            System.out.println(parse);
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.failure(ResultCode.PARAM_TYPE_BIND_ERROR);
        }
        String format = dateFormat.format(parse);
        System.out.println("format:"+format);
        List<HistoryData> all = historyService.findAllByDate(format);
        return  Result.success(all);
    }
}
